package io.cici.cc.mybatis.lite.mapping;

import io.cici.cc.mybatis.lite.session.Configuration;
import io.cici.cc.mybatis.lite.type.TypeHandler;
import io.cici.cc.mybatis.lite.type.TypeHandlerRegistry;
import lombok.Getter;
import lombok.Setter;

/**
 * @author huihui
 */
@Setter
@Getter
public class ResultMapping {

    private Configuration configuration;
    private String property;
    private String column;
    private String type;
    private TypeHandler<?> typeHandler;


    ResultMapping() {
    }


    public static class Builder {
        private final ResultMapping resultMapping = new ResultMapping();

        public Builder(Configuration configuration, String property, String column, String type, TypeHandler<?> typeHandler) {
            resultMapping.configuration = configuration;
            resultMapping.property = property;
            resultMapping.column = column;
            resultMapping.type = type;
            resultMapping.typeHandler = typeHandler;
            if (resultMapping.type == null) {
                throw new NullPointerException();
            }
            if (typeHandler == null) {
                typeHandler = TypeHandlerRegistry.getTypeHandler(resultMapping.type);
                if (typeHandler == null) {
                    throw new NullPointerException();
                }
            }
        }

        public ResultMapping build() {
            return resultMapping;
        }
    }

}
